import requests
from bs4 import BeautifulSoup
from pymongo import MongoClient
def film():
    '''
    存储某电影Top 250数据
    '''
    # 准备基本URL和请求头
    user_agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;)"
    headers = {"User-Agent": user_agent}
    base_url = "https://movie.douban.com/top250?start="

    for i in range(0, 10):
        # 准备全路径
        full_url = base_url + str(i * 25)
        # 发送请求到服务器，返回响应
        response = requests.get(full_url, headers=headers,verify=False)
        html = response.text
        # print(html)
        # 选取符合要求的节点信息
        soup = BeautifulSoup(html, "lxml")
        div_list = soup.find_all('div', {'class': 'info'})
        for node in div_list:
            # 电影名称
            title = node.find('a').find('span').text
            # 电影评分
            score = node.find('div', class_='star').find('span', class_='rating_num').text + '分'
            # 详情链接
            link = node.find('a')['href']
            data_dict = {'电影': title, '评分': score, '链接': link}
            # print(data_dict)
            client = MongoClient("localhost", 27017)
            db = client.spider
            col = db.film250
            # 逐条往集合中插入文档
            col.insert_one(data_dict)
            # 查找score等于9.6的文档
            cursor = col.find({'评分': '9.6分'})
            for doc in cursor:
                print(doc)

if __name__ == '__main__':
    film()
